package com.ruoyi.mqtt.controller;


import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.mqtt.service.MqttPushClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/lbmqtt")
public class MqttSubController {
    @Autowired
    private MqttPushClient mqttPushClient;

    @RequestMapping(value = "/sub",method = RequestMethod.GET)
    @ResponseBody
    public String sub(@RequestParam("top")String top){
        if(StringUtils.isEmpty(top)){
            top = "wendu";
        }
        mqttPushClient.subscribe(top);
        return "订阅成功";
    }

    @RequestMapping(value = "/send",method = RequestMethod.GET)
    @ResponseBody
    public String sendMsg(@RequestParam("msg")String msg,@RequestParam("topic")String topic){
        if(StringUtils.isEmpty(msg)){
            return "消息为空";
        }
        if(StringUtils.isEmpty(topic)){
            topic = "wendu";
        }
        mqttPushClient.publish(topic,msg);
        return "订阅成功";
    }
}